from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
from orders.models import OrderInfo


from jinja2 import Environment


def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': reverse,
        # {% url  %}
    })
    env.filters['new_tel']= new_tel  # {{   value  |  filter}}
    env.filters['pay_method']= pay_method_code_str
    return env


def new_tel(value):
    return value[:3]+'****'+value[7:]

def pay_method_code_str(value):
    # 把支付编码转成描述
    code_str_dict = dict(OrderInfo.PAY_METHOD_CHOICES)
    # {1:'货到付款',2:'支付宝'}
    return code_str_dict[value]